package uit.nfcmenu.utils;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import uit.nfcmenu.models.ItemModel;
import uit.nfcmenu.models.MenuModel;
import uit.nfcmenu.models.OrderModel;


import android.util.Log;

public class JSONParser {
	public static final String tag = JSONParser.class.getSimpleName();

//	public static ArrayList<VMStuff> parseResult(String responseBody) {
//		// Log.i(tag, "parseResult("+responseBody+")");
//		JSONObject jObject = null;
//		ArrayList<VMStuff> VMStuffes = null;
//		try {
//			jObject = new JSONObject(responseBody);
//			JSONArray jArray = jObject.getJSONArray("results");
//			
//			int len = jArray.length();
//			VMStuffes = new ArrayList<VMStuff>(len);	
//			JSONObject jObjTemp = null;
//			VMStuff vmAdress = null;
//			for (int i = 0; i < len; i++) {
//				jObjTemp = jArray.getJSONObject(i).getJSONObject("geometry").getJSONObject("location");
//				vmAdress = new VMStuff(jObjTemp.getDouble("lat"), jObjTemp.getDouble("lng"));
//				VMStuffes.add(vmAdress);
//				//Log.i(tag, "new VMStuff: lat="+vmAdress.mLat+" lon="+vmAdress.mLon);
//			}
//			
//		} catch (JSONException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//				
//		return VMStuffes;
//	}

	/**
	 * Parse data to get MENU
	 * Such as from json:
	 * {
			menu: {
				menuID: "1",
				description: "sdfdsf",
				menuName: "tung",
				subMenuID: "0"
			}
		}
	 * @param response
	 * @return
	 */
	public static ArrayList<MenuModel> parseMenus(String response) {
		// TODO Auto-generated method stub
		ArrayList<MenuModel> models = null;
		if (response != null) {
			JSONObject jo = null;
			try {
				jo = new JSONObject(response);
				JSONArray ja = jo.getJSONArray("menu");
				
				int len = ja.length();
				models = new ArrayList<MenuModel>(len);	
				JSONObject joTemp = null;
				MenuModel model = null;
				for (int i = 0; i < len; i++) {
					joTemp = ja.getJSONObject(i);
					model = new MenuModel();
					model.setMenuID(joTemp.getInt("menuID"));
					model.setImageLink(joTemp.getString("imageLink"));
					model.setMenuName(joTemp.getString("menuName"));
					model.setSubMenuID(joTemp.getInt("subMenuID"));
					models.add(model);
					//Log.i(tag, "get "+model.toString());
				}
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
				
		return models;
	}

	/**
	 * Parse data to get ITEM
	 * Such as from json:
	 * {"item":[
			{
				"itemID":"1",
				"itemName":"milk",
				"menuID":"7",
				"imageLink":"http://restaurant-webservice.appspot.com/serve?blob-key=AMIfv94dzJZkZLwYEcAnEWhjluj2J7jdH0JJrs0KUivfmPVnKAuM1O0GkqiTkwvLM4eO5SS4f1HcnZV1LNYKQXRskH_3x9WqNAa8HPSIZO1O8nckyzL4ir40wR_TFafn1nYKNYIN29SNArcJo49uPI12V2n8vvkAV8MLG97dKPY3C4OewyEQkFk",
				"description":"This is description for item 1",
				"discount":"10"
			},
			...
	 * @param response
	 * @return
	 */
	public static ArrayList<ItemModel> parseItems(String response) {
		// TODO Auto-generated method stub
		ArrayList<ItemModel> models = null;
		if (response != null) {
			JSONObject jo = null;
			try {
				jo = new JSONObject(response);
				JSONArray ja = jo.getJSONArray("item");
				
				int len = ja.length();
				models = new ArrayList<ItemModel>(len);	
				JSONObject joTemp = null;
				ItemModel model = null;
				for (int i = 0; i < len; i++) {
					joTemp = ja.getJSONObject(i);
					model = new ItemModel();
					model.setItemID(joTemp.getInt("itemID"));
					model.setItemName(joTemp.getString("itemName"));
					model.setMenuID(joTemp.getInt("menuID"));
					model.setImageLink(joTemp.getString("imageLink"));
					model.setDescription(joTemp.getString("description"));
					model.setDiscount(joTemp.getInt("discount"));
					models.add(model);
					//Log.i(tag, "get "+model.toString());
				}
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
				
		return models;
	}
	
	/**
	 * Parse data to get ITEM
	 * Such as from json:
	 * {
		orders: [
			{
				itemID: "1",
				orderID: "16002",
				orderTime: "Thu Feb 23 15:12:06 UTC 2012",
				quantity: "1",
				status: "cooking",
				tableNo: "5"
			},
			...
	 * @param response
	 * @return
	 */

	public static ArrayList<OrderModel> parseOrders(String response, String time) {
		// TODO Auto-generated method stub
		ArrayList<OrderModel> items = null;
		if (response != null) {
			JSONObject jo = null;
			try {
				jo = new JSONObject(response);
				JSONArray ja = jo.getJSONArray("orders");
				
				int len = ja.length();
				items = new ArrayList<OrderModel>(len);	
				JSONObject joTemp = null;
				OrderModel item = null;
				for (int i = 0; i < len; i++) {
					joTemp = ja.getJSONObject(i);
					item = new OrderModel();
					item.setItemID(joTemp.getInt("itemID"));
					item.setOrderID(joTemp.getInt("orderID"));
					item.setOrderTime(joTemp.getString("orderTime"));
					item.setQuantity(joTemp.getInt("quantity"));
					item.setStatus(joTemp.getString("status"));
					item.setTableNo(joTemp.getInt("tableNo"));
					item.setComment(joTemp.getString("comment"));
					items.add(item);
					//Log.i(tag, "get "+item.toString());
				}
				CommConstant.Timeupdate= jo.getString("time");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
				
		return items;
	}
}
